﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Windows.Forms;

namespace CayleyTree
{
    public partial class Form1 : Form
    {
        private Graphics graphics;
        double th1 = 30 * Math.PI / 180;
        double th2 = 20 * Math.PI / 180;
        double per1 = 0.6;
        double per2 = 0.7;
        int depth=10;
        double length=100;
        Pen pen = new Pen(Color.Blue);

        public Form1()
        {
            InitializeComponent();
        }
       
        void drawCayleyTree(int n,double x0, double y0, double leng, double th)
        {
            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);

            double y1 = y0 + leng * Math.Sin(th);

            graphics.DrawLine(pen, (int)x0, (int)y0, (int)x1, (int)y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        

        private void button1_Click(object sender, EventArgs e)
        {
            if (graphics == null) graphics = this.panel1.CreateGraphics();
            graphics.Clear(Color.White);
            length = LengthValue.Value;
            th1 = Th1_Value.Value;
            th2 = TH2_Value.Value;
            try
            {
                if(DepthValue.Text!= String.Empty)
                    depth = Convert.ToInt32(DepthValue.Text);
                else depth = 10;
                if (Pr1_Value.Text!= String.Empty)
                    per1 = Convert.ToDouble(Pr1_Value.Text);
                else per1 = 0.6;
                if (Pr2_Value.Text != String.Empty)
                    per2 = Convert.ToDouble(Pr2_Value.Text);
                else per2 = 0.7;
                if (per1 >= 1 || per2 >= 1) throw new Exception("输入错误");
                drawCayleyTree(depth, panel1.Width / 2, panel1.Height, length, -Math.PI / 2);
            }
            catch(Exception)
            {
                Form2 form2 = new Form2();
                form2.ShowDialog();
                th1 = 30 * Math.PI / 180;
                th2 = 20 * Math.PI / 180;
                per1 = 0.6;
                per2 = 0.7;
                //drawCayleyTree(10, panel1.Width / 2, panel1.Height, 100, -Math.PI / 2);
            }
            
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            pen.Color = Color.Red;
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            pen.Color = Color.Yellow;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            pen.Color = Color.Green;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            pen.Color = Color.Blue;
        }

        private void LengthValue_Scroll(object sender, ScrollEventArgs e)
        {
            this.label8.Text = Convert.ToString(LengthValue.Value);
        }

        private void Per1Value_Scroll(object sender, ScrollEventArgs e)
        {
            this.label9.Text = Convert.ToString(Th1_Value.Value);
        }

        private void Per2Value_Scroll(object sender, ScrollEventArgs e)
        {
            this.label10.Text = Convert.ToString(TH2_Value.Value);
        }
    }
}
